package etm.servlet.vivienda;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import upc.condominio.modelo.Vivienda;
import etm.excepcion.DAOExcepcion;
import etm.negocio.GestionQueja;
import etm.negocio.GestionVivienda;


/**
 * Servlet implementation class QuejaInsertaServlet
 */
@WebServlet("/ViviendaInsertUpdateServlet")
public class ViviendaInsertUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViviendaInsertUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Dentro de doPost del servlet ViviendaInsertUpdateServlet");
		
	
		GestionVivienda negocio = new GestionVivienda();
		try {
			
			System.out.println("Ubicacion: " + request.getParameter("cmbUbicacion"));
			System.out.println("cmbTipo: " + request.getParameter("cmbTipo"));
			System.out.println("accion: " + request.getParameter("accion"));
			System.out.println("idViviendaEdit: " + request.getParameter("idViviendaEdit"));
			
			
			Vivienda item = new Vivienda();
			
			item.setNumero(request.getParameter("txtNumero"));
			item.setMetraje(request.getParameter("txtMetraje"));		
			item.getTipoVivienda().setIdTipo(request.getParameter("cmbTipo"));			
			item.getUbicacionVivienda().setIdUbicacion(Integer.parseInt(request.getParameter("cmbUbicacion")));
			
			if (request.getParameter("accion").equals("Nuevo")){
				item.getResidente().setIdResidente(Integer.parseInt(request.getParameter("cmbResidente")));
				negocio.insertar(item);	
			}else{
				item.setIdVivienda(request.getParameter("idViviendaEdit"));
				negocio.actualizar(item);
			}
			
			response.sendRedirect(request.getContextPath()	+ "/ViviendaBuscarServlet");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}
